package fun;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class JTranslate
{
    private final static Map<Character, String> map = getMap();
    
    private static Map<Character, String> getMap()
    {
        if (JTranslate.map != null)
        {
            return JTranslate.map;
        }
        Map<Character, String> map = new HashMap<Character, String>();
        map.put('A', "KA");
        map.put('B', "ZU");
        map.put('C', "MI");
        map.put('D', "TE");
        map.put('E', "KU");
        map.put('F', "LU");
        map.put('G', "JI");
        map.put('H', "RI");
        map.put('I', "KI");
        map.put('J', "ZUS");
        map.put('K', "ME");
        map.put('L', "TA");
        map.put('M', "RIN");
        map.put('N', "TO");
        map.put('O', "MO");
        map.put('P', "NO");
        map.put('Q', "KE");
        map.put('R', "SHI");
        map.put('S', "ARI");
        map.put('T', "CHI");
        map.put('U', "DO");
        map.put('V', "RU");
        map.put('W', "MEI");
        map.put('X', "NA");
        map.put('Y', "FU");
        map.put('Z', "ZI");
        return Collections.unmodifiableMap(map);
    }
    
    private static String reverse(String translated)
    {
        Map<String, String> reverseMap = new HashMap<String, String>();
        for (Entry<Character, String> map : getMap().entrySet())
        {
            reverseMap.put(map.getValue(), map.getKey().toString());
        }
        int index = 0;
        StringBuilder translatedBuilder = new StringBuilder(translated);
        StringBuilder result = new StringBuilder();
        while (index < translated.length())
        {
            int size = 2;
            String key = translatedBuilder.substring(index, index + size);
            if (reverseMap.containsKey(key) || reverseMap.containsKey(key = translatedBuilder.substring(index, index + ++size)))
            {
                result.append(reverseMap.get(key));
                index += size;
            }
            else
            {
                result.append(translatedBuilder.substring(index, ++index));
            }
        }
        return result.toString();
    }
    
    private static String translate(String toTranslate)
    {
        StringBuilder result = new StringBuilder();
        // toUpper
        toTranslate = toTranslate.toUpperCase();
        for (char c : toTranslate.toCharArray())
        {
            String out = String.valueOf(c);
            if (getMap().containsKey(c))
            {
                out = getMap().get(c);
            }
            result.append(out);
        }
        return result.toString();
    }
    
    public static void main(String[] args)
    {
        System.out.println(translate("Sebastian ist der geilste"));
        System.out.println(reverse("ARIKUZUKAARICHIKIKATO KIARICHI TEKUSHI JIKUKITAARICHIKU"));
    }
}
